﻿var Direction = Object.freeze({
    "Top": [-1, 0],
    "Right": [0, 1],
    "Down": [1, 0],
    "Left": [0, -1],
    getKey: function (value) {
        for (var key in this) {
            if (this[key] == value) {
                return key;
            }
        }
        return null;
    }
});

function Point(row, col) {
    this.Row = row;
    this.Col = col;
}

function getDirections(inPoint, outPoint) {

    var directions = [Direction.Top, Direction.Right, Direction.Down, Direction.Left];
    var index = 0;

    if (inPoint.Row - outPoint.Row === 1 && inPoint.Col === outPoint.Col) {
        // Преместено е нагоре
        index = directions.indexOf(Direction.Down);
    } else if (inPoint.Row === outPoint.Row && inPoint.Col - outPoint.Col === -1) {
        // Преместено е надясно
        index = directions.indexOf(Direction.Left);
    } else if (inPoint.Row - outPoint.Row === -1 && inPoint.Col == outPoint.Col) {
        // Преместено е надолу
        index = directions.indexOf(Direction.Top);
    } else {
        // Преместено е наляво
        directions.splice(Direction.Right);
    }

    directions.splice(index, 1);

    return directions;
}


function TraverseAndMarkFieldsForDeleting(matrix, positionPoint, directions) {

    var i = 0;
    var elementsToRemove = new Array(matrix.length);
    i = 0;
    for (; i < matrix.length; i++) {
        elementsToRemove[i] = new Array(matrix.length);
    }
    i = 0;
    var j = 0;
    for (; i < matrix.length; i++) {
        j = 0;
        for (; j < matrix.length; j++) {
            elementsToRemove[i][j] = 0;
        }
    }

    i = 0;
    elementsToRemove[positionPoint.Row][positionPoint.Col] = 1;
    for (; i < 3; i++) {
        var coefHei = directions[i][0];
        var coefWid = directions[i][1];
        j = 0;
        for (; j < 2; j++) {
            var currentHei = positionPoint.Row + coefHei * (j + 1);
            var currentWid = positionPoint.Col + coefWid * (j + 1);
            if (currentHei >= 0 && currentWid >= 0 && currentHei < matrix.length && currentWid < matrix.length) {
                if (matrix[currentHei][currentWid] === matrix[positionPoint.Row][positionPoint.Col]) {
                    elementsToRemove[currentHei][currentWid] = 1;
                }
            }
        }
    }

    return elementsToRemove;
}
